<template>
  <a-row :gutter="10">
    <a-col :md="5" :sm="24">
      <a-card :bordered="false">
        <!-- 按钮操作区域 -->
        <a-row style="margin-left: 14px"> </a-row>
        <div style="background: #fff; padding-left: 16px; height: 100%; margin-top: 5px">
          <a-alert type="info" :showIcon="true">
            <div slot="message">
              当前选择：<span v-if="this.currSelected.title">{{ getCurrSelectedTitle() }}</span>
            </div>
          </a-alert>
          <a-input-search
            @search="onSearch"
            style="width: 100%; margin-top: 10px"
            placeholder="请输入部门名称"
            allowClear
          />
          <!-- 树-->
          <a-col :md="24" :sm="24">
            <a-tree
              @select="onSelect"
              :selectedKeys="selectedKeys"
              :treeData="departTree"
              :expandedKeys="iExpandedKeys"
              @expand="onExpand"
            />
          </a-col>
        </div>
      </a-card>
    </a-col>
    <a-col :md="19" :sm="24">
      <a-card
        :bordered="false"
        :tabList="tabList"
        size="small"
        :activeTabKey="activeTabKey"
        @tabChange="
          (key) => {
            this.activeTabKey = key
          }
        "
      >
        <BrandBussiness ref="bussForm"></BrandBussiness>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
import { queryDepartTreeList1, searchByKeywords } from '@/api/api'
import BrandBussiness from './modules/BrandBussiness'

export default {
  name: 'PosMerchantBussinessList',
  components: {
    BrandBussiness,
  },
  data() {
    return {
      description: '经营范围管理页面',
      iExpandedKeys: [],
      loading: false,
      autoExpandParent: true,
      disable: true,
      treeData: [],
      visible: false,
      departTree: [],
      rootTree: {},
      hiding: true,
      model: {},
      depart: {},
      selectedKeys: [],
      currSelected: {},
      allTreeKeys: [],
      tabList: [
        {
          key: 'BRAND',
          tab: '品牌',
        },
      ],
      activeTabKey: 'BRAND',
    }
  },
  computed: {},
  created() {
    this.refresh()
  },
  methods: {
    loadTree() {
      var that = this
      that.treeData = []
      that.departTree = []
      queryDepartTreeList1().then((res) => {
        if (res.success) {
          //部门全选后，再添加部门，选中数量增多
          this.allTreeKeys = []
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.treeData.push(temp)
            that.departTree.push(temp)
            that.getAllKeys(temp)
            if (i == 0) {
              this.rootTree = temp
            }
          }

          if (this.rootTree) {
            this.queryChild(this.rootTree.key)
          }

          this.loading = false
        }
      })
    },
    getCurrSelectedTitle() {
      console.log('currSelected', this.currSelected)
      let parentName = this.currSelected.parentName
      let departName = !this.currSelected.title ? '' : this.currSelected.title
      if (parentName && parentName != '') {
        return parentName + '/' + departName
      }
      return departName
    },
    refresh() {
      this.loading = true
      this.loadTree()
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys)
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onSearch(value) {
      let that = this
      if (value) {
        searchByKeywords({ keyWord: value }).then((res) => {
          if (res.success) {
            that.departTree = []
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.departTree.push(temp)
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      } else {
        that.loadTree()
      }
    },
    onSelect(selectedKeys, e) {
      console.log('onSelect', selectedKeys, e)
      this.hiding = false
      let record = e.node.dataRef
      console.log('onSelect-record', record)
      this.currSelected = Object.assign({}, record)
      this.queryChild(selectedKeys[0])
    },
    queryChild(orgId, bussType) {
      if(!bussType){
        bussType = this.activeTabKey
      }
      this.$refs.bussForm.query(orgId, bussType)
    },
    getAllKeys(node) {
      // console.log('node',node);
      this.allTreeKeys.push(node.key)
      if (node.children && node.children.length > 0) {
        for (let a = 0; a < node.children.length; a++) {
          this.getAllKeys(node.children[a])
        }
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>